﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.Mvc;
using App.Interface;
using App.Model;
using App.MVC.App_Start;
using Newtonsoft.Json;

namespace App.MVC.Controllers
{
    public class MyCaoZuoController : BaseController
    {
        public IMyCaoZuo _iMyCaoZuo { set; get; }
        public IClass1 _Class1 { set; get; }
        public IJiaoZhiGong _IJiaoZhiGong { set; get; }
        // GET: MyCaoZuo
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetMyKaoHeBiaos()
        {
            var str = _iMyCaoZuo.GetMyGongZuoJiHuas(SiteInfo.GetUserName());
            Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
            var DynamicObject = JsonConvert.DeserializeObject<List<dynamic>>(str, convert);
            List<MyKaoHeBiao> lst = new List<MyKaoHeBiao>();
            foreach (var item in DynamicObject)
            {
                MyKaoHeBiao mk = new MyKaoHeBiao();
                mk.sssj = item.sssj;
                mk.khbzt = item.state;
                mk.cjsj = item.cjsj;
                mk.mkhbid = item.pmid;
                mk.mkhbmc = item.tableName;
                mk.mbid = item.mobanid;
                lst.Add(mk);
            }
            return Json(lst, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetMyLiShiKaoHeBiaos()
        {
            var str = _iMyCaoZuo.GetMyLiShiGongZuoJiHuas(SiteInfo.GetUserName());
            List<MyKaoHeBiao> tt = JsonConvert.DeserializeObject<List<MyKaoHeBiao>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        public string CreateMyKaoHeBiao(string sssj)
        {
            return _iMyCaoZuo.CreateMyGongZuoJiHua(SiteInfo.GetUserName(), sssj);
        }

        public void UpdateMyKaoHeBiao(MyKaoHeBiao model)
        {

            string str = JsonConvert.SerializeObject(model);
            _iMyCaoZuo.UpdateMyGongZuoJiHua(str);
        }

        public void DelMyKaoHeBiao(int khbid)
        {
            _iMyCaoZuo.DelMyKaoHeBiao(khbid);
        }

        public JsonResult GetMyKaoHeBiaoInfo(int khbid)
        {
            string str = _iMyCaoZuo.GetMyKaoHeBiaoInfo(khbid);
            Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str, convert);
            foreach (var item in tt.MyKaoHeBiaoOnes)
            {
                foreach (var item2 in item.MyKaoHeBiaoTwos)
                {
                    var js = _IJiaoZhiGong.GetEmployee(item2.gzcdrid.ToString());
                    if (!string.IsNullOrEmpty(js))
                    {
                        JiaoZhiGong jsmodel = JsonConvert.DeserializeObject<JiaoZhiGong>(js);
                        item2.gzcdr = jsmodel.XingMing;
                    }
                }
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateTiJiaoMyKaoHeBiao(int khbid)
        {
            var str = _iMyCaoZuo.GetMyKaoHeBiaoInfo(khbid);
            MyKaoHeBiao kaohebiao = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            var flag = true;
            dynamic msg = new { data = "", error = false };
            foreach (var item in kaohebiao.MyKaoHeBiaoOnes)
            {
                var yfz = item.fz;
                var sfz = 0m;
                if (item.MyKaoHeBiaoTwos != null && item.MyKaoHeBiaoTwos.Count != 0)
                {
                    foreach (var item2 in item.MyKaoHeBiaoTwos)
                    {
                        sfz = sfz + item2.fz;
                    }
                }
                else
                {
                    sfz = yfz;
                }
                if (yfz != sfz)
                {
                    flag = false;
                    msg = new { data = "项目：‘" + item.mbnr + "’的分值不正确，请重新核对", error = true };
                    break;
                }
            }

            if (flag)
            {
                _iMyCaoZuo.TiJiaoKaoHeBiao(SiteInfo.GetUserName(),khbid);
                //_Class1.BoHuiYiJianYiYue(khbid);
            }
            return Json(msg, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 工作计划完成情况
        /// </summary>
        /// <returns></returns>
        public ActionResult GongZuoJiHuaWanCheng()
        {
            return View();
        }
        /// <summary>
        /// 临时工作
        /// </summary>
        /// <returns></returns>
        public ActionResult LinShiGongZuo()
        {
            return View();
        }
        /// <summary>
        /// 删除临时工作
        /// </summary>
        /// <param name="mkhbid"></param>
        /// <param name="twoid"></param>
        public void DelLinShiGongZuo(int mkhbid, int twoid)
        {
            _iMyCaoZuo.DelLinShiGongZuo(mkhbid, twoid);
        }

        public void CreateLinShiGongZuo(string gznr, string gzbz, int mkhbid)
        {
            //LinShiGongZuo

            _iMyCaoZuo.CreateLinShiGongZuo(SiteInfo.GetUserName(), gznr, gzbz, mkhbid);
        }

        public void UploadFile(string guid, string fileName, string filePath, string uploadtitle, int mkhbid, int twoid, string uploadfileHash)
        {
            _iMyCaoZuo.UploadFile(mkhbid, twoid, guid, filePath + "/" + fileName, uploadtitle, uploadfileHash);
        }

        public JsonResult GetFileList(int mkhbid, int twoid)
        {
            var str = _iMyCaoZuo.GetFileList(mkhbid, twoid);
            List<MyKaoHeBiaotwo_FuJians> tt = JsonConvert.DeserializeObject<List<MyKaoHeBiaotwo_FuJians>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void DelFile(int mkhbid, int twoid, int fjid)
        {
            _iMyCaoZuo.DelFile(mkhbid, twoid, fjid);
        }

        public ActionResult Other()
        {
            return View();
        }
        /// <summary>
        /// 获取工作承担人
        /// </summary>
        /// <returns></returns>
        public JsonResult p_getGongZuoChengDanRen()
        {
            var str = _iMyCaoZuo.getGongZuoChengDanRen(SiteInfo.GetUserName());
            List<dynamic> tt = JsonConvert.DeserializeObject<List<dynamic>>(str);
            List<JiaoZhiGong> lst = new List<JiaoZhiGong>();
            foreach (var item in tt)
            {
                JiaoZhiGong model = new JiaoZhiGong();
                model.XingMing = item.name;
                model.userinfoId = item.id;
                lst.Add(model);
            }
            return Json(lst, JsonRequestBehavior.AllowGet);
        }

        public dynamic saveKaoHeNeiRongTow(MyKaoHeBiaotwo model, int mkhbid, int oneid)
        {
            //model.twolx = 5;saveKaoHeNeiRongTow
            model.cjrid = SiteInfo.UserId;
            if (model.gzcdrid == Guid.Empty)
            {
                model.gzcdrid = Guid.Parse(SiteInfo.UserId);
            }
            string str = JsonConvert.SerializeObject(model);
            if (model.twoid > 0)
            {
                _iMyCaoZuo.updateKaoHeNeiRongTow(str, mkhbid, oneid, SiteInfo.GetUserName());
                return Json(new { id = model.twoid }, JsonRequestBehavior.AllowGet);
            }
            else
            {

                string tt = _iMyCaoZuo.saveKaoHeNeiRongTow(str, mkhbid, oneid, SiteInfo.GetUserName());
                dynamic d = JsonConvert.DeserializeObject<dynamic>(tt);
                dynamic r = new {id = d.id.ToString()};
                return Json(r, JsonRequestBehavior.AllowGet);
            }

        }

        public void delKaoHeNeiRongTow(int towid, int mkhbid, int oneid)
        {
            _iMyCaoZuo.delKaoHeNeiRongTow(towid,  mkhbid,  oneid, SiteInfo.GetUserName());
        }
        /// <summary>
        /// 计算第几周
        /// </summary>
        /// <param name="day">day:要判断的日期</param>
        /// <returns></returns>
        public int 全年第几周(DateTime daytime)
        {
            GregorianCalendar gc = new GregorianCalendar();
            int weekOfYear = gc.GetWeekOfYear(daytime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            return weekOfYear;
        }
        public string GetWeek(DateTime dateTime)
        {
            string week = "";
            switch (dateTime.DayOfWeek.ToString())
            {
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
                case "Sunday":
                    week = "星期日";
                    break;
            }
            return week;
        }
        public JsonResult WeeksOfMonth()
        {
            var 当月第一天 = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
            var 当月最后一天 = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
            List<周> tt = new List<周>();
            周 t = new 周();
            t.星期List = new List<string>();
            while (当月第一天.Date != 当月最后一天.Date)
            {

                var 周数 = 全年第几周(当月第一天);
                t.周数 = 周数;
                t.星期List.Add(GetWeek(当月第一天));
                if (t.星期List.Any(s => s == "星期日"))
                {
                    tt.Add(t);
                    t = new 周();
                }
                当月第一天 = 当月第一天.AddDays(1);
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public class 周
        {
            public 周()
            {
                星期List = new List<string>();
                Is有效 = false;
            }

            public int 周数 { set; get; }
            public List<string> 星期List { set; get; }
            public bool Is有效 { set; get; }
        }
        public ActionResult ZhouJiHua()
        {
            return View();
        }
        /// <summary>
        /// 获取没有分配周数的月工作计划
        /// </summary>
        /// <param name="khbid"></param>
        /// <returns></returns>
        public JsonResult GetWeiFenPeiYueJiHua(int khbid)
        {
            string str = _iMyCaoZuo.获取没有分配周数的月工作计划(khbid);
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取已分配周数的周计划
        /// </summary>
        /// <param name="khbid"></param>
        /// <param name="zhou"></param>
        /// <returns></returns>
        public JsonResult GetYiFenPeiZhouJiHua(int khbid, int zhou)
        {
            string str = _iMyCaoZuo.获取已分配周数的周计划(khbid, zhou);
            MyKaoHeBiao tt = JsonConvert.DeserializeObject<MyKaoHeBiao>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 生成单条周计划
        /// </summary>
        /// <param name="khbid"></param>
        /// <param name="zhou"></param>
        public void CreateDanTiaoZhouJiHua(int khbid, int twoid, int zhou)
        {
            _iMyCaoZuo.生成单条周计划(khbid, twoid, zhou);
        }

        /// <summary>
        /// 取消单条周计划
        /// </summary>
        /// <param name="khbid"></param>
        /// <param name="zhou"></param>
        public void DelDanTiaoZhouJiHua(int khbid, int twoid, int zhou)
        {
            _iMyCaoZuo.取消单条周计划(khbid, twoid, zhou);
        }

        public void updatewczt(int khbid, int twoid, bool flag)
        {
            _iMyCaoZuo.UpdateGongZuoJiHuaWanChengZhuangTai(khbid, twoid, flag);
        }

        #region 工作总结

        public JsonResult getYueGongZuoZongJie(int khbid)
        {
            string str = _iMyCaoZuo.GetYueGongZuoZongJie(khbid);
            zongjieone tt = new zongjieone();
            if (!string.IsNullOrEmpty(str))
            {
                tt = JsonConvert.DeserializeObject<zongjieone>(str);
            }
            tt.Zongjietwos = tt.Zongjietwos.OrderBy(s => s.zongjielx).ToList();
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void delGongZuoZongJie(int khbid, int zjid)
        {
            _iMyCaoZuo.delGongZuoJongJie(khbid, zjid);
        }

        public void saveGongZuoZongJie(zongjietwo model, int khbid)
        {
            if (model.zjtowid <= 0)
            {
                _iMyCaoZuo.saveZouZongJie(model.zongjielx, model.zongjielxstr, model.zongjienr, khbid);
            }
            else
            {
                _iMyCaoZuo.updateGongZuoZongJie(model.zongjielx, model.zongjielxstr, model.zongjienr, khbid,
                    model.zjtowid);
            }
        }
        #endregion
    }
}